package demo;


import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.Calendar;
import java.util.Date;

/**
 * @author : feixiang.li
 * @since : 2025-04-10 17:05
 */
public class MyTest3 {

    public static class Demo {

        public static final String DD = "张三";
        private String name;
        private int age;

        private Demo() {
        }

        public Demo(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static void main(String[] args) {

        System.out.println(getMonth(new Date()));

//        System.out.println(invalidDisposalMonth("2025/6"));
//        System.out.println(invalidDisposalMonth("2025/7"));
//        System.out.println(invalidDisposalMonth("2025/8"));
//        System.out.println(invalidDisposalMonth("2025-01"));
        int usedMonths = getUsedMonths(
                DateUtil.parse("2025--01"),
                DateUtil.parse("2025-07-01"),
                1
        );
        System.out.println(usedMonths);
    }

    public static int getUsedMonths(Date start, Date calculateMonth, Integer alterType) {
        //累计使用期数：=MIN（使用期限，MAX（（计算月份-开始摊销月份+1），0））
//        int monthDiff = (int) DateUtil.beginOfMonth(start, calculateMonth, DateUnit.MS);
//        int monthDiff =(int) ChronoUnit.MONTHS.between(LocalDate.parse(start), LocalDate.parse(calculateMonth));
//        int monthDiff = getMonthDiff(start, calculateMonth);
        //处置类型：当月不摊销
        int monthDiff = getMonthDifference(start, calculateMonth);
        int kk= (int)DateUtil.betweenMonth(start, calculateMonth, true);
        int usedMonths = 1 == alterType
                ? Math.max(0, monthDiff)
                : Math.max(0, monthDiff + 1);
        usedMonths = Math.min(36, usedMonths);
        return usedMonths;
    }

    private static int getMonthDiff(Date start, Date calculateMonth) {
        if (start == null || calculateMonth == null) {
            return 0;
        }
        int yearDiff = DateUtil.year(calculateMonth) - DateUtil.year(start);
        int monthDiff = DateUtil.month(calculateMonth) - DateUtil.month(start);
        return yearDiff * 12 + monthDiff;
    }

    /**
     * 获取两个日期的月份差
     * @param date1 被减数
     * @param date2 减数
     * @return java.lang.Integer 相差的月份
     */
    public static Integer getMonthDifference(Date date1,Date date2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);
        return (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12
                + cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
    }

    private static Integer getMonth(Date date) {
        return new DateTime(date).getMonthOfYear();
    }

    public static boolean invalidDisposalMonth(String dateStr) {
        if (StringUtils.isBlank(dateStr)) {
            return true;
        }
        try {
            Date disposalDate = DateUtil.parse(dateStr, "yyyy/M");
            // 当前月份
            Date now = DateUtil.beginOfMonth(new Date());
            // 【处置月份】必须<=当前月份
            return disposalDate.getTime() > now.getTime();
        } catch (Exception e) {
            System.out.println("日期格式错误");
        }
        return true;
    }

    public static void k2() {
    }

    public static void k1() {
        BigDecimal a = new BigDecimal("14757259.9000");
        BigDecimal b = new BigDecimal("18821626.81");
        BigDecimal c = new BigDecimal("20737571.62");
        BigDecimal d = new BigDecimal("12452157.8800");

        BigDecimal e = a.add(b).subtract(c).subtract(d);
        System.out.println(e);
    }

}
